const formatVoteState = (issue) => {
  if (issue.has_vote) {
    return {
      collect: issue.has_vote.collection,
      like: issue.has_vote.like,
      dislike: issue.has_vote.dis_like,
    };
  }
  return {};
};

const formatVoteCount = issue => ({
  like: issue.votes.like_size,
  dislike: issue.votes.dis_like_size,
});

// 格式化问题数据
export const formatIssue = issue => ({
  id: issue.id,
  title: issue.title, // 问题标题
  content: issue.content, // 问题描述
  tags: issue.tags, // 问题标签
  essence: issue.essence, // 精华帖

  authorId: issue.owner.id, // 问题创建者id
  authorNickname: issue.owner.nickname, // 问题创建者昵称
  authorAvatar: issue.owner.avatar, // 问题创建者头像

  // 2020年4月29日
  author: issue.owner || {},

  voteState: formatVoteState(issue),
  voteCount: formatVoteCount(issue),

  commentsCount: issue.comment_count, // 讨论数

  spaceId: issue.product_id, // 问题所属空间的id
  spaceTitle: issue.product_title, // 问题所属空间的title

  path: `/spaces/${issue.product_id}/issues/${issue.id}`,
  space: {
    id: issue.product_id,
    title: issue.product_title,
  },

  created_at: issue.created_at,
  updated_at: issue.updated_at,
});

// 格式化问题列表数据
export const formatIssuesList = issues => issues.map(space => formatIssue(space));
